//引入axios
import axios from 'axios'
const url = 'http://localhost:8080/'

const http = axios.create({
  baseURL: url,
  timeout: 10000,
});

// 请求拦截器
http.interceptors.request.use(config => {
  // 添加 token 等公共参数
  const token = localStorage.getItem('token');
  console.log(localStorage.getItem('token'));
  if(token){
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

// 响应拦截器
http.interceptors.response.use(
  response => response.data,
  error => {
    // 统一处理错误
    console.error('请求失败:', error.message);
    return Promise.reject(error);
  }
);

export default http;